package com.bayair.doctor.ui.inquiry;

import android.view.View;
import android.widget.ProgressBar;

import com.bayair.doctor.R;
import com.bayair.doctor.adapter.InquiryAdapter;
import com.bayair.doctor.biz.im_helper.IMHelper;
import com.bayair.doctor.data.bean.ServerListResp;
import com.bayair.doctor.mvp.BaseMVPFragment;
import com.bayair.doctor.mvp.annotation.InjectPresenter;
import com.bayair.doctor.ui.chat.ChatActivity;
import com.bayair.doctor.utils.WidgetUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.gturedi.views.StatefulLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo;
import com.tencent.qcloud.tim.uikit.modules.contact.ContactItemBean;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class InquiryFragment extends BaseMVPFragment implements InquiryContract.View, OnRefreshListener {

    @BindView(R.id.rv_mypatient_list)
    RecyclerView mRecyclerView;
    @BindView(R.id.pb_mypatient_loading)
    ProgressBar mProgressBar;
    @BindView(R.id.refreshLayout_inquiry)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.ll_stateful_inquiry)
    StatefulLayout mStatefulLayout;

    @InjectPresenter
    InquiryPresenter mPresenter;

    private InquiryAdapter mAdapter;
    private List<ContactItemBean> mContactData = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_inquiry;
    }

    @Override
    protected void initView() {
        setRecycler();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void setListener() {
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.autoRefresh();
    }


    //初始化recycler view
    private void setRecycler() {
        mAdapter = new InquiryAdapter(mContactData);
        WidgetUtils.bindRecyclerView(mRecyclerView, mAdapter);
        //设置item click
        mAdapter.setOnItemClickListener(this::patientItemClick);
    }

    private void patientItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
        ChatInfo chatInfo = IMHelper.getInstance().contactToChat(mContactData.get(position));
        ChatActivity.actionStart(chatInfo);
    }


    @Override
    public void updateContact(List<ContactItemBean> infos) {
        log("======updateContact========");
        mContactData = infos;
        mAdapter.setNewData(mContactData);

    }

    @Override
    public void onError() {
        log("======onError========"+NetworkUtils.isConnected());
        if (NetworkUtils.isConnected()) {
            mStatefulLayout.showError("刷新出错", this::errorClick);
        } else {
            mStatefulLayout.showOffline(this::errorClick);
        }
        mRefreshLayout.finishRefresh(false);
    }

    @Override
    public void onEmpty() {
        log("======onEmpty========");
        mStatefulLayout.showEmpty("暂无在服务中的患者");
        mRefreshLayout.finishRefresh();
    }

    @Override
    public void onComplete() {
        log("======onComplete========");
        mStatefulLayout.showContent();
        mRefreshLayout.finishRefresh();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        log("======onRefresh========");
        mPresenter.getListInServer();
    }

    private void errorClick(View view) {
        log("======errorClick========");
        mRefreshLayout.finishRefresh();
        mRefreshLayout.autoRefresh(20);
    }

}
